﻿@*
    //THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
    //THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS
    //BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
    //OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
    //OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

*@

@*
  // Copy this file into your ServiceAPI examples folder and then open via /HPRMServiceAPI/examples/RecordRelationship
*@
@using HP.HPTRIM.ServiceModel;
@{
    ViewBag.Title = "Add a Record Relationship";


}

@section helpDescription {
    <p>This is a simple example to demonstrate how to create a record relationship, note:</p>
    <ul>
        <li>the ChildRelationships array, add items to this to create new relationships,</li>
        <li>the RelationType enum, different values are found <a href="~/EnumItem?Enums=RecordRelationshipType">here</a></li>
        <li>we can post the Uri for the RelatedRecordrather than having to use a record object, altough we could add a record object (e.g. {Uri:9000000})</li>
    </ul>
}


<form id="recordRelationship">
    <h4>Choose a Record</h4>
    <input type="text" name="Uri" class="typeahead" data-trim-type="@BaseObjectTypes.Record" />

    <h4>Choose another Record</h4>
    <input type="text" name="RelatedUri" class="typeahead" data-trim-type="@BaseObjectTypes.Record" />

    <input class="btn" type="submit" value="Add" />
</form>


@section scripts {
    <script>


        $().ready(function () {
            $('#recordRelationship').submit(function () {
                var data = { "Uri": $("input[name=Uri]").val() }

                data["ChildRelationships"] = [{
                    "RecordRelationshipRelatedRecord": $("input[name=RelatedUri]").val(),
                    "RecordRelationshipRelationType": "IsRelatedTo"                    
                }];

                $.ajax({
                    url: "Record",
                    type: 'POST',
                    contentType: 'application/json',
                    data: JSON.stringify(data)

                })
                .done(function (response, statusText) {
                    alert("success");
                })
                .fail(function (xhr) {
                    var err = eval("(" + xhr.responseText + ")");
                    alert(err.ResponseStatus.Message);
                });

                return false;
            });
        });

    </script>
}